﻿using SqlSugar;
using TiaoTiaoCode.BasicDbCore;

namespace TiaoTiaoCode.AutoUpdate.WebApi.StartUps
{
    /// <summary>
    /// SqlSugar 启动配置
    /// </summary>
    public static class SqlSugarStartUp
    {
        /// <summary>
        /// 添加SqlSguar注入
        /// </summary>
        /// <param name="_1"></param>
        /// <param name="configuration"></param>
        public static void AddSqlSugarStartup(this IServiceCollection _1, IConfiguration configuration)
        {
            var dbTypeCfg = configuration.GetValue<string>("DbType");

            var dbType = dbTypeCfg switch
            {
                "MySql" => DbType.MySql,
                "SqlServer" => DbType.SqlServer,
                "Sqlite" => DbType.Sqlite,
                _ => DbType.Sqlite
            };

            DbFramework.DbType = dbType;
            DbFramework.UseFileLog = false;
            DbFramework.Connection = configuration.GetConnectionString(dbTypeCfg);
            var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TiaoTiaoCode.AutoUpdate.DbCore.dll");
            DbFramework.AddDbCoreStartupByFile(path, "TiaoTiaoCode.AutoUpdate.DbCore.Entities", true);
        }
    }
}
